﻿namespace GuiaTest
{
    using System;
    using System.Windows.Automation;
    using Guia.Core;
    using Guia.Locator;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    //TODO: Find a way to write sensible tests. Condition.Equals() seems not to be implemented right
    /// <summary>
    /// Summary description for SearchConditionTest
    /// </summary>
    [TestClass]
    public class SearchConditionTest : AbstractTest
    {
        [TestMethod]
        public void TestByAutomationId()
        {
            SearchCondition searchCondition = SearchCondition.ByAutomationId("myAutomationId");
            Assert.IsNotNull(searchCondition);
            Condition condition = searchCondition.CreateCondition();
            Assert.IsNotNull(condition);
            Assert.IsTrue(condition.GetType() == typeof(PropertyCondition));
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestByAutomationIdWithNull()
        {
            SearchCondition searchCondition = SearchCondition.ByAutomationId(null);
        }

        [TestMethod]
        public void TestByClassName()
        {
            SearchCondition searchCondition = SearchCondition.ByClassName("myClassName");
            Assert.IsNotNull(searchCondition);
            Condition condition = searchCondition.CreateCondition();
            Assert.IsNotNull(condition);
            Assert.IsTrue(condition.GetType() == typeof(PropertyCondition));
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestByClassNameWithNull()
        {
            SearchCondition searchCondition = SearchCondition.ByClassName(null);
        }

        [TestMethod]
        public void TestByName()
        {
            SearchCondition searchCondition = SearchCondition.ByName("myName");
            Assert.IsNotNull(searchCondition);
            Condition condition = searchCondition.CreateCondition();
            Assert.IsNotNull(condition);
            Assert.IsTrue(condition.GetType() == typeof(PropertyCondition));
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestByNameWithNull()
        {
            SearchCondition searchCondition = SearchCondition.ByName(null);
        }

        [TestMethod]
        public void TestByControlType()
        {
            SearchCondition searchCondition = SearchCondition.ByControlType(ControlType.Window);
            Assert.IsNotNull(searchCondition);
            Condition condition = searchCondition.CreateCondition();
            Assert.IsNotNull(condition);
            Assert.IsTrue(condition.GetType() == typeof(PropertyCondition));
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestByControlTypeWithNull()
        {
            SearchCondition searchCondition = SearchCondition.ByControlType(null);
        }

        [TestMethod]
        public void TestByAutomationProperty()
        {
            SearchCondition searchCondition =
                SearchCondition.ByAutomationProperty(AutomationElement.AutomationIdProperty, "myProperty");
            Assert.IsNotNull(searchCondition);
            Condition condition = searchCondition.CreateCondition();
            Assert.IsNotNull(condition);
            Assert.IsTrue(condition.GetType() == typeof(PropertyCondition));
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestByAutomationPropertyWithNull1()
        {
            SearchCondition searchCondition = SearchCondition.ByAutomationProperty(null, new object());
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestByAutomationPropertyWithNull2()
        {
            SearchCondition searchCondition =
                SearchCondition.ByAutomationProperty(AutomationElement.AutomationIdProperty, null);
        }

        [TestMethod]
        public void TestFromAutomationCondition()
        {
            Condition condition = new PropertyCondition(AutomationElement.NameProperty, "name");
            Assert.IsNotNull(condition);
            Assert.IsTrue(condition.GetType() == typeof(PropertyCondition));
            SearchCondition searchCondition = SearchCondition.FromAutomationCondition(condition);
            Assert.IsNotNull(searchCondition);
            Assert.AreEqual(TreeScope.Descendants, searchCondition.Scope);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestFromAutomationConditionWithNull()
        {
            SearchCondition searchCondition = SearchCondition.FromAutomationCondition(null);
        }

        [TestMethod]
        public void TestAnd()
        {
            SearchCondition searchCondition = SearchCondition.ByControlType(ControlType.Window).And(SearchCondition.ByAutomationId("myAutomationId"));
            Assert.IsNotNull(searchCondition);
            Condition condition = searchCondition.CreateCondition();
            Assert.IsNotNull(condition);
            Assert.IsTrue(condition.GetType() == typeof(AndCondition));
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestAndWithNull()
        {
            SearchCondition searchCondition = SearchCondition.ByControlType(ControlType.Window).And(SearchCondition.ByAutomationId("myAutomationId"));
            searchCondition.And(null);
        }

        [TestMethod]
        public void TestCreateCondition()
        {
            SearchCondition searchCondition =
                SearchCondition.FromAutomationCondition(new PropertyCondition(AutomationElement.AutomationIdProperty,
                                                                              "myProperty"));
            Assert.IsNotNull(searchCondition);
            Condition condition = searchCondition.CreateCondition();
            Assert.IsNotNull(condition);
            Assert.IsTrue(condition.GetType() == typeof(PropertyCondition));
        }

        [TestMethod]
        public void TestCreateConditionMultiple()
        {
            SearchCondition searchCondition = SearchCondition.ByAutomationId("automationId").And(SearchCondition.ByControlType(ControlType.Button));
            Assert.IsNotNull(searchCondition);
            Condition condition = searchCondition.CreateCondition();
            Assert.IsNotNull(condition);
        }
    }
}
